
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rt.schedulebase.dto.DictItemGroupDetailDto;
import com.rt.schedulebase.entity.DictItemGroupDetail;
import com.rt.schedulebase.mapper.DictItemGroupDetailMapper;
import com.rt.schedulenew.api.IDictItemGroupDetailService;
import com.rt.schedulenew.api.IExamSyslogService;
import com.rt.schedulenew.utils.base.CommonUtil;
import com.rt.schedulenew.utils.base.ConstantsUtil;
import com.rt.schedulenew.utils.base.LogUtil;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import com.rt.schedulenew.utils.global.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictItemGroupDetailServiceImpl extends ServiceImpl<DictItemGroupDetailMapper, DictItemGroupDetail> implements IDictItemGroupDetailService {
    @Autowired
    private IExamSyslogService syslogService;

    @Override
    public JsonResult deleteItemGroupDetail(List<DictItemGroupDetailDto> dtoList) {
        int count = 0;
        List<String> succList = new ArrayList<String>();
        List<String> failList = new ArrayList<String>();
        for (DictItemGroupDetailDto dto : dtoList) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("ITEM_NAME", dto.getItemName());
            map.put("ITEM_CODE", dto.getItemCode());
            map.put("GROUP_CODE", dto.getGroupCode());
            QueryWrapper<DictItemGroupDetail> qw = new QueryWrapper<>();
            qw.allEq(map);
            boolean deleteResult = remove(qw);
            if (!deleteResult) {
                failList.add("数据:" + dto.getItemName() + "删除失败");
                ++count;
            } else {
                succList.add(dto.getItemName() + "删除成功");
            }
        }
        String desc = "[项目分组明细] " + StringUtils.join(succList, ",") + " , " + StringUtils.join(failList, ",");
        syslogService.saveLog(ConstantsUtil.ModuleName.ITEM_GROUP.name(), ConstantsUtil.OperateName.DELETE.getDesc(), desc);
        if (count > 0) {
            return JsonResultUtil.failure(failList.toString());
        }
        return JsonResultUtil.success("删除成功");
    }

    @Override
    public JsonResult insertGroupDetail(List<DictItemGroupDetailDto> list) {
        int count = 0;
        List<String> succList = new ArrayList<>();
        List<String> failList = new ArrayList<>();
        for (DictItemGroupDetailDto dto : list) {
            boolean insertResult = save((DictItemGroupDetail) dto);
            if (!insertResult) {
                failList.add("项目:" + dto.getItemName() + "添加失败");
                ++count;
            } else {
                succList.add(dto.getItemName() + "添加成功");
            }
        }
        String desc = "[项目分组明细] " + StringUtils.join(succList, ",") + " , " + StringUtils.join(failList, ",");
        try {
            syslogService.saveLog(ConstantsUtil.ModuleName.ITEM_GROUP.name(), ConstantsUtil.OperateName.INSERT.getDesc(), desc);
        } catch (Exception e) {
            log.error("insertGroupDetail:操作日志写入错误!", e);
        }
        if (count > 0) {
            return JsonResultUtil.failure(failList.toString());
        }
        return JsonResultUtil.success("添加成功");
    }

    @Override
    public JsonResult updateGroupDetail(DictItemGroupDetailDto dto) {
        DictItemGroupDetail dig = new DictItemGroupDetail();
        dig.setScheduleOrder(dto.getScheduleOrder());
        UpdateWrapper<DictItemGroupDetail> update = new UpdateWrapper<>();
        update.set(dto.getScheduleOrder() == null, "SCHEDULE_ORDER", "");
        update.eq("GROUP_CODE", dto.getGroupCode());
        update.in("ITEM_CODE", CommonUtil.split2List(dto.getItemCode()));
        DictItemGroupDetail old = getOne(update);
        boolean success = update(dig, update);
        if (success) {
            DictItemGroupDetail newDetail = getOne(update);
            String desc = "[项目分组明细] " + old.getItemName() + ":" + LogUtil.compareObject(old, newDetail);
            syslogService.saveLog(ConstantsUtil.ModuleName.ITEM_GROUP.name(), ConstantsUtil.OperateName.UPDATE.getDesc(), desc);
            return JsonResultUtil.success("修改成功");
        }
        return JsonResultUtil.failure("修改失败");
    }

    @Override
    public JsonResult getItemGroupDetailList(DictItemGroupDetailDto dto) {
        Page<?> page = PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        if (dto.getItemName() != null) {
            dto.setItemName(dto.getItemName().replace(" ", ""));
        }
        if (dto.getItemCode() != null) {
            dto.setItemCode(dto.getItemCode().replace(" ", ""));
        }
        Map<String, Object> map = new HashMap<>();
        List<DictItemGroupDetail> list = null;
        QueryWrapper<DictItemGroupDetail> qw = new QueryWrapper<>();
        if (dto.getGroupCode() != null) {
            map.put("GROUP_CODE", dto.getGroupCode());
            qw.allEq(map)
                    .like(dto.getItemName() != null, "ITEM_NAME", dto.getItemName())
                    .like(dto.getItemCode() != null, "ITEM_CODE", dto.getItemCode());
            list = list(qw);
        }
        PageVO pageVO = new PageVO();
        pageVO.setPageNo(dto.getPageNo());
        pageVO.setPageSize(dto.getPageSize());
        pageVO.setTotal(page.getTotal());
        pageVO.setPages(page.getPages());
        return JsonResultUtil.success("成功", list, pageVO);
    }
}
